<template>
  <div>
    <el-form
      ref="form"
      :model="form"
      :rules="rules"
      label-width="80px"
      style="width: 500px; margin: 2px"
    >
      <el-form-item label="事故编号" prop="number">
        <el-input v-model="form.number"></el-input>
      </el-form-item>
      <el-form-item label="事故类型" prop="recordTypeId">
        <el-select
          v-model="form.recordTypeId"
          placeholder="请选择事故类型"
          style="width: 419px"
        >
          <el-option label="工程事故" value="1"></el-option>
          <el-option label="安全事故" value="2"></el-option>
          <el-option label="消防事故" value="3"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="发生地点" prop="scene">
        <el-input v-model="form.scene"></el-input>
      </el-form-item>
      <el-form-item label="发生时间" prop="datetime">
        <el-col :span="11">
          <el-date-picker
            type="date"
            placeholder="选择日期"
            v-model="form.datetime"
            style="width: 419px"
          ></el-date-picker>
        </el-col>
      </el-form-item>
      <el-form-item label="责任人" prop="person">
        <el-input v-model="form.person"></el-input>
      </el-form-item>
      <el-form-item label="事故图片" prop="picture">
        <el-upload
          style="width: 100px"
          class="avatar-uploader"
          action="https://jsonplaceholder.typicode.com/posts/"
          :show-file-list="false"
        >
          <img v-if="imageUrl" :src="imageUrl" class="avatar" />
          <i v-else class="el-icon-plus avatar-uploader-icon"></i>
        </el-upload>
      </el-form-item>
      <el-form-item label="事故详情" prop="accident">
        <el-input type="textarea" v-model="form.accident"></el-input>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="add">保存</el-button>
        <el-button @click="list" style="color: green">取消</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>
<script>
import { addRecord } from "@/api/safeManage/Record";
export default {
  data() {
    return {
      form: {
        number: "",
        recordTypeId: "",
        scene: "",
        datetime: "",
        person: "",
        picture: "1",
        accident: "",
      },
      rules: {
        number: [
          { required: true, message: "请输入事故编号", trigger: "blur" },
        ],
        recordTypeId: [
          { required: true, message: "请选择事故类型", trigger: "change" },
        ],
        scene: [{ required: true, message: "请输入发生地点", trigger: "blur" }],
        datetime: [
          { required: true, message: "请选择发生时间", trigger: "change" },
        ],
        person: [{ required: true, message: "请输入责任人", trigger: "blur" }],
        picture: [
          { required: true, message: "请选择事故图片", trigger: "change" },
        ],
        accident: [
          { required: true, message: "请输入事故详情", trigger: "blur" },
        ],
      },
    };
  },
  methods: {
    //添加
    add() {
      this.$refs.form.validate((valid) => {
        if (valid) {
          addRecord(this.form).then((res) => {
            if (res.data) {
              this.$message({
                message: "添加成功",
                type: "success",
              });
              this.$router.push("/SafeManage/Record");
            } else {
              this.$message.error("添加失败");
              return;
            }
          });
        } else {
          console.log("error submit!!");
          return false;
        }
      });
    },
    //跳转
    list() {
      this.$router.push("/SafeManage/Record");
    },
  },
  created() {},
};
</script>
<style>
.avatar-uploader .el-upload {
  border: 1px dashed #d9d9d9;
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
}
.avatar-uploader .el-upload:hover {
  border-color: #409eff;
}
.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 178px;
  height: 178px;
  line-height: 178px;
  text-align: center;
}
.avatar {
  width: 178px;
  height: 178px;
  display: block;
}
</style>